//经纬度转墨卡托
function latLng2WebMercator2(lng, lat) {
    var x = lng * 20037508.34 / 180;
    var y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
    y = y * 20037508.34 / 180;
    return { x, y };
}
function computerTimediff(endTime) {
    if (!endTime) {
        return 0
    } else {
        var ts = endTime - new Date();
        if (ts > 0) {
            var hh = parseInt(ts / 1000 / 60 / 60 % 24, 10);
            var mm = parseInt(ts / 1000 / 60 % 60, 10);
            var ss = parseInt(ts / 1000 % 60, 10);
            hh = hh < 10 ? ("0" + hh) : hh;   //时
            mm = mm < 10 ? ("0" + mm) : mm;   //分
            ss = ss < 10 ? ("0" + ss) : ss;   //秒
            return `${hh}:${mm}:${ss}`
        } else {
            return "打击结束"
        }

    }
}
//转整数
function myParseInt(val) {
    curVal = val * 1;
    if (curVal) {

        return parseInt(curVal)

    } else {
        return val
    }

}
/**转树形结构
 * 
 */
function toTree(data,all) {
    let gropList = data.groupList;
    // console.log(data,44);
    gropList.forEach(ele => {
        //变成树形结构 把字段名统一
        ele.meshId = ele.groupId;
        ele.meshName = ele.groupName;
    });
    //单独成为一组
    let meshList = data.meshList;
    meshList.forEach(ele => {
        gropList.push({ meshName: ele.meshName, meshId: ele.meshId })
    })
    let newGroup = {}
    newGroup.meshId = 0;
    newGroup.meshName = all;
    newGroup.memberList = gropList;

    return [newGroup];
}
//
//树形转列表

//转列表
function toList(data) {
    let list = [];
    let groupList = data.groupList;
    let meshList = data.meshList;
    /*   console.log(groupList,'groupList',meshList,'meshList') */
    groupList.forEach(ele => {
        //变成树形结构 把字段名统一
        ele.memberList.forEach(it => {
            list.push({ deviceName: it.meshName, deviceId: it.meshId, list: it.list, groupName: ele.groupName, meshIp: it.meshIp, statusList: it.statusList })
        })
    });
    meshList.forEach(ele => {
        list.push({ deviceName: ele.meshName, deviceId: ele.meshId, list: ele.list, meshIp: ele.meshIp, statusList: ele.statusList })
    })
    /*  console.log(list,'list') */
    return list
}
//距离转成带单位
function distanceConvert(num) {
    num = num * 1
    if (num >= 1000) {
        let remain = num % 1000;
        if (!remain) {
            return num / 1000 + 'km'
        } else {

            return (num / 1000).toFixed(1) + "km"
        }
    } else {
        return num + "m"
    }
}
//通过地址获取图片尺寸

function getSizeByUrl(imgUrl) {
    return new Promise((resolve, reject) => {
        let img = new Image()
        let obj = {}
        img.onload = function () {
            obj = {
                width: this.width,
                height: this.height,

            }
            resolve(obj)
        }
        img.src = imgUrl
    })

}
/**
 * 
 * @param {*} oldList  原数据
 * @param {*} newList  新数据
 * @param {*} key      比较键名
 * @returns 
 */
//比较新旧数据
function compareOldAndNew(oldList, newList, key) {
    //克隆一份 数据防止被篡改
    newList = JSON.parse(JSON.stringify(newList))
    let someArr = [];   //相同的
    for (let i = 0; i < newList.length; i++) {
        for (let j = 0; j < oldList.length; j++) {
            if (oldList[j][key] === newList[i][key]) {
                someArr.push(newList[i]);
            }
        }
    }
    let deleteArr = [];  //删除的
    for (let i = 0; i < someArr.length; i++) {
        for (let j = 0; j < oldList.length; j++) {
            if (someArr[i][key] === oldList[j][key]) {
                oldList.splice(j, 1);    //去重
            }
        }
    }
    deleteArr = oldList;
    let addArr = [];  //增加的
    for (let i = 0; i < someArr.length; i++) {
        for (let j = 0; j < newList.length; j++) {
            if (someArr[i][key] === newList[j][key]) {
                newList.splice(j, 1);    //去重
            }
        }
    }
    addArr = newList;
    let resList = []
    if (addArr.length) {

        addArr.map(it => {
            it.flag = 'add'
            resList.push(it)
        })
    }
    if (deleteArr.length) {
        deleteArr.map(it => {
            it.flag = 'delete'
            resList.push(it)
        })
    }
    if (someArr.length) {
        someArr.map(it => {
            it.flag = 'same'
            resList.push(it)
        })
    }
    return resList
}

/**
 * @function 获取浏览器地址的参数
 * @param {String} [name=''] 获取值的key,默认为空,为空时，函数返回json对象
 * @param {String} [url=window.location.href] 地址，默认为浏览器的url
 * @example  getParams('aa', 'https://www.baidu.com?aa=101')       //101
 */
const getParams = (name = '', url = window.location.href) => {
    let endVal = '';
    if (url.indexOf("?") != -1) {   //判断url中有没有出现?
        let arrs = url.substring(url.indexOf("?") + 1).split('&');  //截取字符串，获取到?后面的那部分内容;以&符号为准，分割成数组
        arrs = arrs.filter(item => item);     //原有数组过滤为空的值
        let obj = {};
        for (let i = 0; i < arrs.length; i++) {
            let index = arrs[i].indexOf("=");
            let keys = arrs[i].substring(0, index);       //第一个等号前面的那部分
            let vals = arrs[i].substring(index + 1);      //第一个等号后面的内容
            if (keys) {     //keys不为空时，追加到obj对象里
                obj[keys] = decodeURI(vals);    //解码输出,值含有中文字符的情况
            }
        }
        if (arrs.length > 0) {  //先判断数组是不是为空，在判断name是不是有值传过来
            if (name) {
                endVal = obj[name] || '';
            } else {
                endVal = obj;
            }
        }
    }
    return endVal;
}
//rgb转16进制 rgb(255,255,255)=> #FFF=>16777215 
const rgbConvertInt = function (oldColor) {
    if (!oldColor) {
        return
    }
    oldColor = oldColor.replace('rgb(', '')
    oldColor = oldColor.replace(')', '')
    let list = oldColor.split(',')
    let newColor = ''
    list.forEach(item => {
        let it = (item * 1).toString(16) + ''
        //如果只有一个就doubel
        if (it.length <= 1) {
            it += it
        }
        newColor += it

    });

    return parseInt(newColor, 16)
}


